home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / Palette&GWorld (Fat) / Palette&GWorld.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-15  |  7.0 KB  |  274 lines  |  [TEXT/MMCC]

  1. /*  Includes  */
  2. #include <QDOffscreen.h>
  3. #include <Palettes.h>
  4.  
  5. /*  Defines  */
  6. #define    TOTALCOLORS    256
  7. #define    WWIDTH        (TOTALCOLORS * 2)
  8. #define    WHALFWIDTH    TOTALCOLORS
  9. #define    WHEIGHT        TOTALCOLORS
  10. #define WLEFT        (((qd.screenBits.bounds.right - qd.screenBits.bounds.left) - WWIDTH) / 2)
  11. #define WTOP        (((qd.screenBits.bounds.bottom - qd.screenBits.bounds.top) - WHEIGHT) / 2)
  12.  
  13. /*  Global Variable Definitions  */
  14. #ifdef    powerc
  15.     QDGlobals             qd;
  16. #endif
  17. WindowPtr            gWindow;
  18. PaletteHandle        gPalette;
  19. GWorldPtr            gRGBGWorld;
  20. GWorldPtr            gPmGWorld;
  21. PixMapHandle        gRGBPixMap;
  22. PixMapHandle        gPmPixMap;
  23. CTabHandle            gClut;
  24. Rect                gRGBRect = { 0, 0, WHEIGHT, WHALFWIDTH };
  25. Rect                gPmRect  = { 0, WHALFWIDTH, WHEIGHT, WWIDTH };
  26. short                gMode    = srcOr;
  27. short                gText    = times;
  28. short                gSize    = 24;
  29.  
  30. /*  Procedure Prototypes  */
  31. void initMac( void );
  32. void makePalette( void );
  33. void createWindow( void );
  34. void createOffscreens( void );
  35. void createRGBForeColorImage( void );
  36. void createPmForeColorImage( void );
  37. void drawWindow( void );
  38. void doEventLoop( void );
  39.  
  40.  
  41. /*             */
  42. /*  Main loop. */
  43. /*             */
  44.  
  45. main( void )
  46. {
  47.     initMac();
  48.     
  49.     makePalette();
  50.     createWindow();    
  51.     createOffscreens();
  52.     createRGBForeColorImage();
  53.     createPmForeColorImage();
  54.  
  55.     doEventLoop();
  56. }
  57.  
  58.  
  59. /*                      */
  60. /*  Mac initialization. */
  61. /*                      */
  62.  
  63. void initMac( void )
  64. {
  65.     MaxApplZone();
  66.     MoreMasters();
  67.     MoreMasters();
  68.     InitGraf( &qd.thePort );
  69.     InitFonts();
  70.     InitWindows();
  71.     InitMenus();
  72.     TEInit();
  73.     InitDialogs( (long)nil );
  74.     InitCursor();
  75.     FlushEvents( 0, everyEvent );
  76. }
  77.  
  78.  
  79. /*                                    */
  80. /*  Create the palette from the clut. */
  81. /*                                    */
  82.  
  83. void makePalette( void )
  84. {    
  85.     short    plttID = 128;
  86.     
  87.     /*  Get the color table from the .rsrc file. */
  88.     gClut = GetCTable( plttID );
  89.     
  90.     /*  Create the palette from the color table.                                                  */
  91.     /*  pmExplicit says we want the colors in the same order as the color table.                  */
  92.     /*  pmTolerant with a tolerance of 0 says we want exactly the same colors as the color table. */
  93.     gPalette = NewPalette( TOTALCOLORS, gClut, pmExplicit + pmTolerant, 0 );
  94. }
  95.  
  96.  
  97. /*                                                                     */
  98. /*  Create the window to display the images and set the palette to it. */
  99. /*                                                                     */
  100.  
  101. void createWindow( void )
  102. {
  103.     Rect    bounds;
  104.     
  105.     SetRect( &bounds, WLEFT, WTOP, WLEFT + WWIDTH, WTOP + WHEIGHT );
  106.     
  107.     gWindow = NewCWindow( 0L, &bounds, "\pPalette&GWorld", true, documentProc,
  108.                             (WindowPtr)-1L, true, 0L );                        
  109.     SetPort( gWindow );
  110.     
  111.     /*  Attach the palette to the window. */
  112.     NSetPalette( gWindow, gPalette, pmAllUpdates );
  113. }
  114.  
  115.  
  116. /*                                                           */
  117. /*  Create the GWorlds based on the clut to hold the images. */
  118. /*                                                           */
  119.  
  120. void createOffscreens( void )
  121. {
  122.     CTabHandle    ctable;
  123.     GDHandle    screensDevice;
  124.     Rect        area;
  125.     short        depth = 8;
  126.  
  127.     /*  Set the color table's ctSeed equal to that of the main screen's GDevice. */ 
  128.     /*  This tells QuickDraw not to do color mapping.                            */ 
  129.     /*  Color mapping is not needed because the palette matches the color table. */ 
  130.     screensDevice = GetMainDevice();
  131.     if (screensDevice != nil)
  132.         (**gClut).ctSeed = (**(**(**screensDevice).gdPMap).pmTable).ctSeed;
  133.         
  134.     /*  Create the RGBForeColor offscreen world using the colortable. */
  135.     NewGWorld( &gRGBGWorld, depth, &gRGBRect, gClut, nil, 0 );
  136.     gRGBPixMap = GetGWorldPixMap( gRGBGWorld );    
  137.  
  138.     /*  Create the PmForeColor offscreen world using the colortable. */
  139.     NewGWorld( &gPmGWorld, depth, &gPmRect, gClut, nil, 0 );
  140.     gPmPixMap = GetGWorldPixMap( gPmGWorld );    
  141. }
  142.  
  143.  
  144. /*                                                              */
  145. /*  Create the GWorld image using Index2Color and RGBForeColor. */
  146. /*                                                              */
  147.  
  148. void createRGBForeColorImage( void )
  149. {
  150.     int            i;
  151.     CGrafPtr    currentPort;
  152.     GDHandle    currentDevice;
  153.     RGBColor    color;
  154.     FontInfo    info;
  155.     Str255        string = "\pRGBForeColor";
  156.     
  157.     GetGWorld( ¤tPort, ¤tDevice );
  158.     
  159.     SetGWorld( gRGBGWorld, nil );
  160.     PenSize ( 1, 1 );
  161.     LockPixels( gRGBPixMap );
  162.     
  163.     /*  Show all the colors in the background using Index2Color and RGBForeColor. */
  164.     for (i = 0; i < TOTALCOLORS; i++){
  165.         Index2Color( i, &color );
  166.         RGBForeColor( &color );
  167.         MoveTo( i, 0 );
  168.         Line( 0, WHEIGHT );
  169.     }
  170.     
  171.     /*  Draw label; RGBForeColor should now be black. */
  172.     TextMode( gMode );
  173.     TextFont( gText );
  174.     TextSize( gSize );        
  175.     GetFontInfo( &info );
  176.     MoveTo( (WHALFWIDTH - StringWidth(string))/2, WHEIGHT/2 - info.ascent/3 );
  177.     DrawString( string );
  178.     
  179.     UnlockPixels( gRGBPixMap );
  180.     SetGWorld( currentPort, currentDevice );
  181. }
  182.  
  183.  
  184. /*                                                                                      */
  185. /*  Create the GWorld image by setting the palette to the GWorld and using PmForeColor. */
  186. /*                                                                                      */
  187.  
  188. void createPmForeColorImage( void )
  189. {
  190.     int            i;
  191.     CGrafPtr    currentPort;
  192.     GDHandle    currentDevice;
  193.     FontInfo    info;
  194.     Str255        string = "\pPmForeColor";
  195.     
  196.     GetGWorld( ¤tPort, ¤tDevice );
  197.     
  198.     SetGWorld( gPmGWorld, nil );
  199.     NSetPalette( (WindowPtr)gPmGWorld, gPalette, pmNoUpdates );
  200.     PenSize ( 1, 1 );
  201.     LockPixels( gPmPixMap );
  202.     
  203.     /*  Show all the colors in the background using PmForeColor. */
  204.     for (i = 0; i < TOTALCOLORS; i++){
  205.         PmForeColor( i );
  206.         MoveTo( (i + WHALFWIDTH), 0 );
  207.         Line( 0, WHEIGHT );
  208.     }
  209.                     
  210.     /*  Draw label; PmForeColor should now be black. */
  211.     TextMode( gMode );
  212.     TextFont( gText );
  213.     TextSize( gSize );        
  214.     GetFontInfo( &info );
  215.     MoveTo( ((WHALFWIDTH - StringWidth(string))/2) + WHALFWIDTH, WHEIGHT/2 - info.ascent/3 );
  216.     DrawString( string );
  217.     
  218.     UnlockPixels( gPmPixMap );
  219.     SetGWorld( currentPort, currentDevice );
  220. }
  221.  
  222.  
  223. /*                                      */
  224. /*  CopyBits both images to the screen. */
  225. /*                                      */
  226.  
  227. void drawWindow( void )
  228. {    
  229.     CopyBits( (BitMap*)*gRGBPixMap, &gWindow->portBits, &(**gRGBPixMap).bounds,
  230.                 &gRGBRect, srcCopy, 0l );
  231.                 
  232.     CopyBits( (BitMap*)*gPmPixMap, &gWindow->portBits, &(**gPmPixMap).bounds,
  233.                 &gPmRect, srcCopy, 0l );
  234. }
  235.  
  236.  
  237. /*              */
  238. /*  Event loop. */
  239. /*              */
  240.  
  241. void doEventLoop( void )
  242. {
  243.     EventRecord event;
  244.     WindowPtr   window;
  245.     short       clickArea;
  246.     Rect        screenRect;
  247.  
  248.     for (;;){
  249.         if (WaitNextEvent( everyEvent, &event, 0, nil )){
  250.             if (event.what == mouseDown){
  251.                 clickArea = FindWindow( event.where, &window );
  252.                 
  253.                 if (clickArea == inDrag){
  254.                     screenRect = (**GetGrayRgn()).rgnBBox;
  255.                     DragWindow( window, event.where, &screenRect );
  256.                 }
  257.                 else if (clickArea == inContent){
  258.                     if (window != FrontWindow())
  259.                         SelectWindow( window );
  260.                 }
  261.                 else if (clickArea == inGoAway)
  262.                     if (TrackGoAway( window, event.where ))
  263.                         return;
  264.             }
  265.             else if (event.what == updateEvt){
  266.                 window = (WindowPtr)event.message;    
  267.                 SetPort( window );                
  268.                 BeginUpdate( window );
  269.                 drawWindow();
  270.                 EndUpdate( window );
  271.             }
  272.         }
  273.     }
  274. }